/*
 * app_hub_api_def.h
 *
 *  Created on: 2023年7月27日
 *      Author: HXF
 */

#ifndef APP_HUB_API_DEF_H_
#define APP_HUB_API_DEF_H_

#include "device.h"

#define HUB_VER (1)
#define HUB_API_USART_MSG_MAX_LEN (255)
#define HUB_API_USART_MSG_LEN_MIN_VALUE (4)
#define HUB_API_USART_MSG_DATA_MAX_LEN (HUB_API_USART_MSG_MAX_LEN - HUB_API_USART_MSG_LEN_MIN_VALUE - 4)
#define HUB_API_USART_MSG_LEN_MAX_VALUE (HUB_API_USART_MSG_DATA_MAX_LEN + HUB_API_USART_MSG_LEN_MIN_VALUE)

#define HUB_API_RF_MSG_MAX_LEN (255)
#define HUB_API_RF_MSG_DATA_MAX_LEN (HUB_API_RF_MSG_MAX_LEN - 2)

#define CAT_IO_BITMASK_BYTE ((DEVICE_TYPE_IO_CN/8)+((DEVICE_TYPE_IO_CN%8)?1:0))
#define CAT_SOUNDER_BITMASK_BYTE ((DEVICE_TYPE_SOUNDER_CN/8)+((DEVICE_TYPE_SOUNDER_CN%8)?1:0))
#define CAT_KEYPAD_BITMASK_BYTE ((DEVICE_TYPE_KEYPAD_CN/8)+((DEVICE_TYPE_KEYPAD_CN%8)?1:0))
#define CAT_FOB_BITMASK_BYTE ((DEVICE_TYPE_FOB_CN/8)+((DEVICE_TYPE_FOB_CN%8)?1:0))

#define HUB_NEWVER_USE  (1)
#define HUB_SYNC_PRM_SAVE (1)
//#define HUB_AS_TEST_MACHINE (1)
typedef enum{
  SOFT_RESET = 0x00,
  XMODEM_RESET = 0x01,
}HUB_API_RESET_MODE_E;

typedef enum{
  EXIT_LOCAL_MODE = 0x00,
  ENTER_LOCAL_MODE = 0x01,
}HUB_API_LOCAL_MODE_E;

typedef enum{
  NEED_INIT = 0x01,
  BCAST_FINISH = 0x02,
  P2P_DO_FINISH = 0x03,
  NEED_SYNC_PRM = 0x04,
  JAMMING_EVT = 0x10,
}HUB_API_HUB_EVT_TYPE_E;

typedef enum{
  NEED_FILE_TRANSPORT = 0x01,
  FILE_FINISH_TRANSPORT = 0x02,
}HUB_API_FILE_EVT_TYPE_E;

typedef enum{
  CAT_IO = 0x01,
  CAT_SOUNDER,
  CAT_KEYPAD,
  CAT_FOB,
}CAT_TYPE_E;

typedef enum{
  GROUP_IDX_KEY = 0x03,
  IOPUT_KEY = 0x10,
  TEMPHUMI_KEY = 0x12,
  VOLTCPOWER_KEY = 0x13,
  RUNTIMEENERGY_KEY = 0x14,
  OPTION_KEY = 0x50,
  OPTION_VAR_KEY = 0x51,
}STATU_MODEL_KEY_E;

typedef enum{
  COMLED_CTRL_KEY = 0x60,
  OPERATE_CTRL_KEY = 0x61,
  KEYVALUE_GET_KEY = 0x70,
}OPERATE_MODEL_KEY_E;

typedef enum{
  BC_IOEN_BTYPE = 0x01,
  BC_SOUNDER_BTYPE = 0x02,
  BC_KEYPAD_BTYPE = 0x03,
  BC_SWITCH_BTYPE = 0x04,
  BC_UPDATEME_BTYPE = 0x08,
}BC_BTYPE_KEY_E;

typedef enum{
  TEST_TONE_CTRL = 0x01,
  SET_CTUNE_CTRL = 0x02,
  SET_LFXO_CTUNE_CTRL = 0x03,
  LFXO_CLK_CTRL = 0x04,
  GET_PANID_CTRL = 0x05,
  SET_PANID_CTRL = 0x06,
}TEST_CTRL_E;

typedef enum{
  P_TO_H_SET_INIT = 0x01,
  P_TO_H_SET_RFM_QRFH = 0x02,
  P_TO_H_SET_RFM_QRFH_BATCH = 0x03,
  P_TO_H_SET_RFM_STATUS_MODEL = 0x04,
  P_TO_H_SET_RFM_OPERATE_MODEL = 0x05,
  P_TO_H_SET_RFM_SYNC_OPTION_BATCH = 0x06,
  P_TO_H_SET_RFM_SYNC_QRFH_BATCH = 0x07,
  P_TO_H_SET_HUB_RESET = 0x10,

  P_TO_H_GET_REGISTERED_RFM_INFO = 0x30,
  P_TO_H_GET_SPECIFIC_RFM_INFO = 0x31,
  P_TO_H_GET_HUB_PHYSICAL = 0x32,
  P_TO_H_GET_HUB_RSSI = 0x33,
  P_TO_H_GET_HUB_VER = 0x34,

  P_TO_H_DO_LOCAL_REGISTER = 0x60,
  P_TO_H_DO_MAC_REGISTER = 0x61,
  P_TO_H_DO_QRCODE_REGISTER = 0x62,
  P_TO_H_DO_SYNC_BROADCAST = 0x63,
  P_TO_H_DO_NORMAL_BROADCAST = 0x64,
  P_TO_H_DO_POINT_TO_POINT = 0x65,
  P_TO_H_DO_DELETE_RFM = 0x69,
  P_TO_H_DO_START_OTA = 0x6A,
  P_TO_H_DO_SN_REGISTER = 0x6B,
  P_TO_H_DO_FILE_TRANSPORT = 0x6C,
  P_TO_H_DO_CHOOSE_ANT = 0x6D,
  P_TO_H_DO_STOP_SYNC_BROADCAST = 0x6E,
  P_TO_H_DO_TEST = 0x80,

  H_TO_P_EVENT_HUB_EVENT = 0xA0,
  H_TO_P_EVENT_RFM_EVENT = 0xA1,
  H_TO_P_EVENT_FILE_TRANSFER = 0xA2,
  H_TO_P_EVENT_RFM_DEBUG_EVENT = 0xAF,
}HUB_API_USART_MSG_FCODE_E;

typedef enum{
  NO_ERROR,
  PARAMETER_ERROR,
  NOREGISTER_ERROR,
  RFM_BUSY_ERROR,
  POINT_TO_POINT_ERROR,
  PKT_CRC16_ERROR,
  DO_BUSY_ERROR,
  FILE_CRC32_ERROR,
  DO_USELESS_ERROR,  //该行为没有意义
}HUB_API_FCODE_ERRORCODE_E;


/*   定义串口收的结构体            */
typedef struct{
  unsigned char freq;
  unsigned char jamming_th;
  unsigned char cust_code;
}usart_rx_set_init_data_t;

typedef struct{
  unsigned char cat_type;
  unsigned char no;
  unsigned char qrfh1;
  unsigned char qrfh2;
}usart_rx_set_rfm_qrfh_data_t;

typedef struct{
  unsigned char cat_type;
  unsigned char qrf_bit_idx;
  unsigned char bit_mask[HUB_API_USART_MSG_DATA_MAX_LEN - 2];
}usart_rx_set_rfm_qrfh_batch_data_t;

typedef struct{
  unsigned char cat_type;
  unsigned char no;
  unsigned char key_value[HUB_API_USART_MSG_DATA_MAX_LEN - 2];
}usart_rx_set_rfm_status_model_data_t;

typedef struct{
  unsigned char cat_type;
  unsigned char no;
  unsigned char key_value[HUB_API_USART_MSG_DATA_MAX_LEN - 2];
}usart_rx_set_rfm_operate_model_data_t;

typedef struct{
  unsigned char number;
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN - 1];
}usart_rx_set_rfm_sync_option_batch_data_t;

typedef struct{
  unsigned char number;
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN - 1];
}usart_rx_set_rfm_sync_qrfh_batch_data_t;

typedef struct{
  unsigned char reset_mode;
}usart_rx_set_hub_reset_data_t;

typedef struct{
  unsigned char cat_type;
  unsigned char no;
}usart_rx_get_registered_rfm_data_t;

typedef struct{
  unsigned char cat_type;
  unsigned char no;
}usart_rx_get_specific_rfm_info_data_t;

typedef struct{
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN];
}usart_rx_get_hub_physical_data_t;

typedef struct{
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN];
}usart_rx_get_hub_rssi_data_t;

typedef struct{
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN];
}usart_rx_get_hub_ver_data_t;

typedef struct{
  unsigned char ctrl_data;
}usart_rx_do_local_register_data_t;

typedef struct{
  unsigned char mac[MAC_ADDRESS_LENGTH];
}usart_rx_do_mac_register_data_t;

typedef struct{
  unsigned char mac[MAC_ADDRESS_LENGTH];
  unsigned char qr_key[16];
}usart_rx_do_qrcode_register_data_t;

typedef struct{
  unsigned char brty;
  //unsigned char ctrl;
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN - 1];
}usart_rx_do_sync_broadcast_data_t;

typedef struct{
  unsigned char brty;
  //unsigned char ctrl;
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN - 1];
}usart_rx_do_normal_broadcast_data_t;

typedef struct{
  unsigned char cat_type;
  unsigned char no;
  unsigned char key_value[HUB_API_USART_MSG_DATA_MAX_LEN - 2];
}usart_rx_do_point_to_point_data_t;

typedef struct{
  unsigned char cat_type;
  unsigned char no;
}usart_rx_do_delete_rfm_data_t;

typedef struct{
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN];
}usart_rx_do_start_ota_data_t;

typedef struct{
  unsigned char sn[SN_LENGTH];
}usart_rx_do_sn_register_data_t;

typedef struct{
  unsigned char page[2];
  unsigned char data[224];
  unsigned char check_code[4];
}usart_rx_do_file_transport_data_t;

typedef struct{
  unsigned char ant_mode;
  unsigned char ant_idx;
}usart_rx_do_choose_ant_data_t;

typedef struct{
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN];
}usart_rx_do_stop_sync_broadcast_data_t;

typedef struct{
  unsigned char ctrl;
  unsigned char data_len;
  unsigned char data[8];
}usart_rx_do_test_data_t;

typedef struct{
  unsigned char error_code;
}usart_rx_event_hub_event_data_t;

typedef struct{
  unsigned char error_code;
}usart_rx_event_rfm_event_data_t;

typedef struct{
  unsigned char error_code;
}usart_rx_event_file_event_data_t;

typedef struct{
  unsigned char error_code;
}usart_rx_event_rfm_debug_event_data_t;

typedef struct{
  unsigned char error_code;
}usart_rx_error_code_data_t;


/*   定义串口发收的结构体            */
typedef struct{
  unsigned char error_code;
}usart_tx_set_init_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_set_rfm_qrfh_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_set_rfm_qrfh_batch_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_set_rfm_status_model_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_set_rfm_operate_model_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_set_rfm_sync_option_batch_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_set_rfm_sync_qrfh_batch_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_set_hub_reset_data_t;

typedef struct{
  unsigned char info[16];
}usart_tx_get_registered_rfm_data_t;

typedef struct{
  unsigned char qrfr1;
  unsigned char qrfr2;
  unsigned char qrfh1;
  unsigned char qrfh2;
  unsigned char option[3];
}usart_tx_get_specific_rfm_info_data_t;

typedef struct{
  unsigned char v_integer;
  unsigned char v_decimal;
  unsigned char t_integer;
  unsigned char t_decimal;
}usart_tx_get_hub_physical_data_t;

typedef struct{
  unsigned char real_rssi;
  unsigned char avg_rssi;
}usart_tx_get_hub_rssi_data_t;

typedef struct{
  unsigned char version_1;
  unsigned char version_2;
  unsigned char version_3;
  unsigned char yy;
  unsigned char mm;
  unsigned char dd;
  unsigned char no;
}usart_tx_get_hub_ver_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_local_register_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_mac_register_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_qrcode_register_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_sync_broadcast_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_normal_broadcast_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_point_to_point_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_delete_rfm_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_start_ota_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_sn_register_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_file_transport_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_choose_ant_data_t;

typedef struct{
  unsigned char error_code;
}usart_tx_do_stop_sync_broadcast_data_t;

typedef struct{
  unsigned char error_code;
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN - 1];
}usart_tx_do_test_data_t;

typedef struct{
  unsigned char event_type;
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN - 1];
}usart_tx_event_hub_event_data_t;

typedef struct{
  unsigned char cat_type;
  unsigned char no;
  unsigned char device_type;
  unsigned char model_type;
  unsigned char hard_type;
  unsigned char rolling_code[4];
  unsigned char rssi;
  unsigned char debug_info[2];
  unsigned char cmd;
  unsigned char payload[HUB_API_USART_MSG_DATA_MAX_LEN - 13];
}usart_tx_event_rfm_event_data_t;

typedef struct{
  unsigned char event_type;
  unsigned char data[HUB_API_USART_MSG_DATA_MAX_LEN - 1];
}usart_tx_event_file_event_data_t;

typedef struct{
  unsigned char cat_type;
  unsigned char no;
  unsigned char device_type;
  unsigned char model_type;
  unsigned char hard_type;
  unsigned char rolling_code[4];
  unsigned char rssi;
  unsigned char debug_info[2];
  unsigned char cmd;
  unsigned char payload[HUB_API_USART_MSG_DATA_MAX_LEN - 13];
}usart_tx_event_rfm_debug_event_data_t;

typedef struct{
  unsigned char code;
}usart_tx_error_code_data_t;

typedef struct{
  unsigned char stx;  //0x02
  unsigned char seq;
  unsigned char len;  //= STAU+FCODE+DATA+CRC[2]
  unsigned char statu;
  unsigned char fcode;
  union{
    usart_rx_set_init_data_t                 rx_set_init_data;
    usart_rx_set_rfm_qrfh_data_t             rx_set_rfm_qrfh_data;
    usart_rx_set_rfm_qrfh_batch_data_t       rx_set_rfm_qrfh_batch_data;
    usart_rx_set_rfm_status_model_data_t     rx_set_rfm_status_model_data;
    usart_rx_set_rfm_operate_model_data_t    rx_set_rfm_operate_model_data;
    usart_rx_set_rfm_sync_option_batch_data_t rx_set_rfm_sync_option_batch_data;
    usart_rx_set_rfm_sync_qrfh_batch_data_t  rx_set_rfm_sync_qrfh_batch_data;
    usart_rx_set_hub_reset_data_t            rx_set_hub_reset_data;

    usart_rx_get_registered_rfm_data_t       rx_get_registered_rfm_data;
    usart_rx_get_specific_rfm_info_data_t    rx_get_specific_rfm_info_data;
    usart_rx_get_hub_physical_data_t         rx_get_hub_physical_data;
    usart_rx_get_hub_rssi_data_t             rx_get_hub_rssi_data;
    usart_rx_get_hub_ver_data_t              rx_get_hub_ver_data;

    usart_rx_do_local_register_data_t        rx_do_local_register_data;
    usart_rx_do_mac_register_data_t          rx_do_mac_register_data;
    usart_rx_do_qrcode_register_data_t       rx_do_qrcode_register_data;
    usart_rx_do_sync_broadcast_data_t        rx_do_sync_broadcast_data;
    usart_rx_do_normal_broadcast_data_t      rx_do_normal_broadcast_data;
    usart_rx_do_point_to_point_data_t        rx_do_point_to_point_data;
    usart_rx_do_delete_rfm_data_t            rx_do_delete_rfm_data;
    usart_rx_do_start_ota_data_t             rx_do_start_ota_data;
    usart_rx_do_sn_register_data_t           rx_do_sn_register_data;
    usart_rx_do_file_transport_data_t        rx_do_file_transport_data;
    usart_rx_do_choose_ant_data_t            rx_do_choose_ant_data;
    usart_rx_do_stop_sync_broadcast_data_t   rx_do_stop_sync_broadcast_data;
    usart_rx_do_test_data_t                  rx_do_test_data;

    usart_rx_error_code_data_t               rx_usart_error_code_data;

    usart_tx_set_init_data_t                 tx_set_init_data;
    usart_tx_set_rfm_qrfh_data_t             tx_set_rfm_qrfh_data;
    usart_tx_set_rfm_qrfh_batch_data_t       tx_set_rfm_qrfh_batch_data;
    usart_tx_set_rfm_status_model_data_t     tx_set_rfm_status_model_data;
    usart_tx_set_rfm_operate_model_data_t    tx_set_rfm_operate_model_data;
    usart_tx_set_rfm_sync_option_batch_data_t tx_set_rfm_sync_option_batch_data;
    usart_tx_set_rfm_sync_qrfh_batch_data_t  tx_set_rfm_sync_qrfh_batch_data;
    usart_tx_set_hub_reset_data_t            tx_set_hub_reset_data;

    usart_tx_get_registered_rfm_data_t       tx_get_registered_rfm_data;
    usart_tx_get_specific_rfm_info_data_t    tx_get_specific_rfm_info_data;
    usart_tx_get_hub_physical_data_t         tx_get_hub_physical_data;
    usart_tx_get_hub_rssi_data_t             tx_get_hub_rssi_data;
    usart_tx_get_hub_ver_data_t              tx_get_hub_ver_data;

    usart_tx_do_local_register_data_t        tx_do_local_register_data;
    usart_tx_do_mac_register_data_t          tx_do_mac_register_data;
    usart_tx_do_qrcode_register_data_t       tx_do_qrcode_register_data;
    usart_tx_do_sync_broadcast_data_t        tx_do_sync_broadcast_data;
    usart_tx_do_normal_broadcast_data_t      tx_do_normal_broadcast_data;
    usart_tx_do_point_to_point_data_t        tx_do_point_to_point_data;
    usart_tx_do_delete_rfm_data_t            tx_do_delete_rfm_data;
    usart_tx_do_start_ota_data_t             tx_do_start_ota_data;
    usart_tx_do_sn_register_data_t           tx_do_sn_register_data;
    usart_tx_do_file_transport_data_t        tx_do_file_transport_data;
    usart_tx_do_choose_ant_data_t            tx_do_choose_ant_data;
    usart_tx_do_stop_sync_broadcast_data_t   tx_do_stop_sync_broadcast_data;
    usart_tx_do_test_data_t                  tx_do_test_data;

    usart_tx_event_hub_event_data_t          tx_event_hub_event_data;
    usart_tx_event_rfm_event_data_t          tx_event_rfm_event_data;
    usart_tx_event_file_event_data_t         tx_event_file_event_data;
    usart_tx_event_rfm_debug_event_data_t    tx_event_rfm_debug_event_data;

    usart_tx_error_code_data_t               tx_usart_error_code_data;
    unsigned char                            data[HUB_API_USART_MSG_DATA_MAX_LEN];
  }data;
  unsigned char crc[2]; // crc 16
  unsigned char etx; //0x03
}hub_api_usart_msg_t;


typedef struct{
  unsigned char len;
  unsigned char cmd;
  unsigned char                            data[HUB_API_RF_MSG_DATA_MAX_LEN];
}hub_api_rf_msg_t;

typedef enum{
  EVT_USART_RX,
  EVT_RF_RX,
}HUB_API_RX_EVT_E;

typedef enum{
  EVT_USART_TX,
  EVT_RF_TX,
}HUB_API_TX_EVT_E;

#pragma pack(1)
typedef struct{
  HUB_API_RX_EVT_E channel;
  union{
    hub_api_usart_msg_t usart_msg;
  }msg;
}hub_api_rx_evt_t;
#pragma pack()


#pragma pack(1)
typedef struct{
  HUB_API_TX_EVT_E channel;
  union{
    hub_api_usart_msg_t usart_msg;
  }msg;
}hub_api_tx_evt_t;
#pragma pack()

typedef int (*hub_api_usart_rx_fun)(const hub_api_usart_msg_t *ptr_pkt, hub_api_usart_msg_t *ptr_ack_pkt);

typedef struct{
  HUB_API_USART_MSG_FCODE_E fcode;
  hub_api_usart_rx_fun fun;
}hub_api_usart_rx_entry_t;

#define HUB_API_TEST  (0)

extern bool hub_log_enable;

#endif /* APP_HUB_API_DEF_H_ */
